/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mz2d.render;

import java.applet.Applet;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

/**
 *
 * @author Meanz
 */
public class MZ2DApplet extends Applet
        implements Runnable, MouseListener, MouseMotionListener, KeyListener, FocusListener, WindowListener {

    public MZ2DFrame window;
    private boolean running = false;
    public int width;
    public int height;
    public Graphics graphics;
    public int clickX;
    public int clickY;
    public int clickType;

    public MZ2DApplet() {
    }

    public final void createFrame(int width, int height) {
        this.width = width;
        this.height = height;
        window = new MZ2DFrame(this, width, height);
        graphics = getDrawingComponent().getGraphics();
        startRunnable(this, 1);
    }

    public final void createApplet(int width, int height) {
        this.width = width;
        this.height = height;
        graphics = getDrawingComponent().getGraphics();
        startRunnable(this, 1);
    }

    public void run() {
        getDrawingComponent().addMouseListener(this);
        getDrawingComponent().addMouseMotionListener(this);
        getDrawingComponent().addKeyListener(this);
        getDrawingComponent().addFocusListener(this);
        long last = System.currentTimeMillis();
        long lastGarbage = System.currentTimeMillis();
        running = true;
        int frameRate = 60;
        gameStart();
        System.err.println("[MZ2D Applet] Initialized");
        while (running) {
            if ((System.currentTimeMillis() - last) > (1000 / frameRate)) {
                gameUpdate();
                gameRender();
                last = System.currentTimeMillis();
                try {
                    Thread.sleep((1000 / frameRate));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if ((System.currentTimeMillis() - lastGarbage > 60000)) {
                System.gc();
                lastGarbage = System.currentTimeMillis();
            }
        }
        System.exit(0);
    }

    public Component getDrawingComponent() {
        if (window != null) {
            return window;
        } else {
            return this;
        }
    }

    public void startRunnable(Runnable runnable, int priority) {
        Thread thread = new Thread(runnable);
        thread.start();
        thread.setPriority(priority);
    }

    public void gameUpdate() {
    }

    public void gameRender() {
    }

    public void gameStart() {
    }

    public void windowActivated(WindowEvent e) {
    }

    public void windowDeactivated(WindowEvent e) {
    }

    public void windowIconified(WindowEvent e) {
    }

    public void windowDeiconified(WindowEvent e) {
    }

    public void windowClosed(WindowEvent e) {
    }

    public void windowClosing(WindowEvent e) {
    }

    public void windowOpened(WindowEvent e) {
    }

    public void focusGained(FocusEvent e) {
    }

    public void focusLost(FocusEvent e) {
    }

    public void keyReleased(KeyEvent k) {
    }

    public void keyTyped(KeyEvent k) {
    }

    public void keyPressed(KeyEvent k) {
    }

    public void mousePressed(MouseEvent e) {
        clickX = e.getX() - 8;
        clickY = e.getY() - 24;
        clickType = e.getButton();
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseDragged(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseMoved(MouseEvent e) {
    }
}
